/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.web.bean.HtmlItem;
import com.jb.web.bean.ListBean;
import com.jb.web.bean.TableCell;
import com.jb.web.bean.TableRow;
import com.jb.web.bean.inspector.PropertyInfo;
import com.jb.web.context.WebContext;

import java.util.List;

import com.jb.faces.component.UIComponent;


public class ListRenderer extends DefaultRenderer {
    /**
     * @param pCtx
     * @param itemToRender
     * @return
     */
    public StringBuffer render(WebContext pCtx, UIComponent itemToRender) {
        if (!isEditable(pCtx, (HtmlItem) itemToRender)) {
            return renderRuntime(pCtx, itemToRender);
        } else {
            return renderDesignTime(pCtx, itemToRender);
        }
    }

    /**
     * @param pCtx
     * @param itemToRender
     * @return
     */
    public StringBuffer renderDesignTime(WebContext pCtx, UIComponent itemToRender) {
        StringBuffer html = new StringBuffer("");
        ListBean hItem = (ListBean) itemToRender;

        html.append("<div id='\"+tableData.getClientId()+\"div'><TABLE ").append(getEditScript(pCtx, hItem))
            .append(addTagProperty("style", hItem.getStyle(pCtx), null))
            .append(addTagProperty("class", hItem.getStyleClass(), null)).append(">");

        if (hItem.isShowHeader() && (hItem.getHeader(pCtx) != null)) {
            html.append(hItem.getHeader(pCtx).render(pCtx));
        }

        for (int i = 0; i < hItem.getRows(); i++) {
            try {
                TableRow row = hItem.getRow(i);
                html.append(row.render(pCtx));
            } catch (Exception e) {
                pCtx.addErrorMessage(hItem, e.getMessage());
            }
        }

        if (hItem.getRows() == 0) {
            html.append("<tr><td>At least one row for a list</td></tr> ");
        }

        if (hItem.isShowFooter() && (hItem.getFooter(pCtx) != null)) {
            html.append(hItem.getFooter(pCtx).render(pCtx));
        }

        html.append("</Table></div>");

        if (hItem.isShowMetaData() && !pCtx.isRunTime()) {
            html.append(renderMetaData(pCtx, hItem));
        }

        return html;
    }

    public String renderMetaData(WebContext pCtx, UIComponent itemToRender) {
        StringBuffer html = new StringBuffer("<table>");
        ListBean hItem = (ListBean) itemToRender;
        List list = hItem.getMetaData();

        for (int i = 0; i < list.size(); i++) {
            PropertyInfo property = (PropertyInfo) list.get(i);
            html.append("<tr><td>").append(property.getPropertyName()).append("</td><td>")
                .append(property.getPropertyType()).append("</td></tr>");
        }

        html.append("</table>");

        return html.toString();
    }

    /**
     * @param pCtx
     * @param itemToRender
     * @return
     */
    public StringBuffer renderRuntime(WebContext pCtx, UIComponent itemToRender) {
        StringBuffer html = new StringBuffer("");
        ListBean hItem = (ListBean) itemToRender;

        if (!isVisible(pCtx, hItem)) {
            return html;
        }

        html.append("<div id='\"+tableData.getClientId()+\"div'><TABLE ").append(getEditScript(pCtx, hItem))
            .append(addTagProperty("style", hItem.getStyle(pCtx), null))
            .append(addTagProperty("class", hItem.getStyleClass(), null)).append(">");

        int iStartAt = hItem.getStart();
        int iEndAt = iStartAt + hItem.getBlockSize();

        if (hItem.isShowHeader() && (hItem.getHeader(pCtx) != null)) {
            html.append(hItem.getHeader(pCtx).render(pCtx));
        }

        if (hItem.getSize() > 0) {
            TableRow row1 = hItem.getRow(0);
            TableRow row2 = hItem.getRow(1);
            TableRow row = row1;
            int oldSeq = hItem.getSequence();
            int rowno = 0;

            for (int i = 0; i < hItem.getSize(); i++) {
                if ((i >= iStartAt) && (i < iEndAt)) {
                    row = row1;

                    if (((rowno % 2) == 1) && (row2 != null)) {
                        row = row2;
                    }

                    hItem.setSequence(i);
                    hItem.setCurrentRow(hItem.get(i));
                    row.setClientId("r_" + hItem.getId() + "_" + i);
                    row.setSequence(i);

                    if (!isEmptyString(hItem.getSelectedStyle())) {
                        if (hItem.getSelected() == i) {
                            row.setBackgroundStyle(hItem.getSelectedStyle());
                            if(!isEmptyString(hItem.getHighlightStyle()))
                            {
                                row.setOnMouseOver("this.style.cssText='"+hItem.getHighlightStyle()+"'");
                                row.setOnMouseOut("this.style.cssText='"+hItem.getSelectedStyle()+"'");
                            }
                        } else {
                            row.setBackgroundStyle(null);
                            if(!isEmptyString(hItem.getHighlightStyle()))
                            {
                                row.setOnMouseOver("this.style.cssText='"+hItem.getHighlightStyle()+"'");
                                row.setOnMouseOut("this.style.cssText=''");
                            }
                        }
                    }else {
                        if(!isEmptyString(hItem.getHighlightStyle()))
                        {
                            row.setOnMouseOver("this.style.cssText='"+hItem.getHighlightStyle()+"'");
                            row.setOnMouseOut("this.style.cssText=''");
                            
                        }                        
                    }
                    html.append(row.render(pCtx));
                    hItem.setCurrentRow(null);
                    rowno++;
                }
            }
            hItem.setSequence(oldSeq); //reset sequence here
        } else {
            TableRow row1 = hItem.getRow(0);
            TableCell cell = (row1 == null) ? null : row1.getCellAt(0);

            if (cell != null) {
                int cols = row1.getCols();
                html.append("<tr><td colspan=\"" + cols + "\" style=\"" + cell.getStyle(pCtx) + "\" class=\"" +
                    cell.getStyleClass() + "\" >");
            } else {
                html.append("<tr><td >");
            }

            html.append(evaluate(pCtx, hItem, hItem.getDefaultValue()));
            html.append("</td></tr>");
        }

        if (hItem.isShowFooter() && (hItem.getFooter(pCtx) != null)) {
            html.append(hItem.getFooter(pCtx).render(pCtx));
        }

        html.append("</table></div>");

        return html;
    }
}
